package com.victor.controller;

import com.victor.dao.BlogDao;
import com.victor.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BlogController {

    @Autowired
    private BlogDao blogDao;
    @RequestMapping("/index/{pageNo}")
    public String index(ModelMap modelMap, @PathVariable("pageNo") int pageNo){
        PageRequest pageRequest = PageRequest.of(pageNo,2);
        Page<Article> page = blogDao.findAll(pageRequest);
        modelMap.addAttribute("page",page);
        modelMap.addAttribute("previousPage",pageRequest.previousOrFirst().getPageNumber());
        modelMap.addAttribute("nextPage",pageRequest.next().getPageNumber());
        modelMap.addAttribute("lastPage",page.getTotalPages()-1);
        return "index";
    }

}
